package com.zhang.family_doctor.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.family_doctor.dto.ServicesDto;
import com.zhang.family_doctor.entity.R;
import com.zhang.family_doctor.entity.Services;
import com.zhang.family_doctor.mapper.ServicesMapper;
import com.zhang.family_doctor.service.ServicesService;
import com.zhang.family_doctor.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ServicesServiceImpl implements ServicesService {

    @Autowired
    private ServicesMapper servicesMapper;

    /**
     * 新增服务项目
     * @param services
     * @return
     */
    @Override
    public R<String> save(Services services) {

        services.setId(CommonUtils.genUUID());
        services.setNumber(CommonUtils.genUUID());
        services.setCreateTime(new Date());
        services.setUpdateTime(new Date());
        Integer frequency = services.getFrequency();
        if (frequency == null){
            return R.buildFailure("次数不能为空不能填空格");
        }

        servicesMapper.save(services);

        return R.success("新增成功！");
    }

    /**
     * 修改服务项目
     * @param services
     * @return
     */
    @Override
    public R<String> update(Services services) {

        services.setUpdateTime(new Date());
        servicesMapper.update(services);
        return R.success("修改成功！");
    }

    /**
     * 分页查询服务项目
     * @param servicesDto
     * @return
     */
    @Override
    public R<PageInfo<Services>> getList(ServicesDto servicesDto) {

        PageHelper.startPage(servicesDto);
        List<Services> servicesList = servicesMapper.getList(servicesDto);
        PageInfo<Services> servicesPageInfo = new PageInfo<>(servicesList);

        return R.success(servicesPageInfo);
    }

    /**
     * 根据id删除服务项目
     * @param id
     * @return
     */
    @Override
    public R<String> delete(Integer id) {

        servicesMapper.delete(id);

        return R.success("删除成功！");
    }
}
